<template>
	<view>
		<view class="u-font-30 u-m-30">
			请上传微信二维码
		</view>
		<view class="box u-m-30">
			<u-upload class="u-font-30" :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1"
				multiple :maxCount="1"></u-upload>
		</view>
			<view class="submit-btn" @click="bindzfb">
				绑定
			</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				fileList1: [],
				wximage:''
			};
		},
		onLoad() {
			this.init()
		},
		methods: {
			init(){
				this.$api.post('/updateuserpay',{}).then(res=>{
					if(res.code == 1){
						let obj = {
							url:this.$oss(res.data.wechat_image)
						}
						this.fileList1.push(obj)
					}
				})
			},
			bindzfb(){
			
				this.$api.post('/updateuserpay',{type:'update',wechat_image:this.wximage}).then(res=>{
					if(res.code == 1){
						this.$toast(res.msg)
						setTimeout(()=>{
							this.$back()
						},1000)
					}
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				let that = this
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: that.$uploadUrl, // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							type: 'image',
							token: uni.getStorageSync('token')
						},
						success: (res) => {
									
							let data = JSON.parse(res.data)
							this.wximage = data.data.url
							if (res.statusCode === 200 && data.code === 1 && data.data) {
								resolve(
									data.data.url
								)
							} else {
								resolve()
							}
									
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fff;
	}
	.box{
		
	}
</style>